package com.baymax.exception.handler;

import com.baymax.entity.dto.R;
import com.baymax.exception.GlobalException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 全局异常捕获
 *
 * @author baymax
 * @date 2023-03-18 15:39
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有不可知的异常
     */
    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public Object handleThrowableException(Throwable e){
        return R.fail(e.getMessage());
    }

    /**
     * 处理全局的异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(Exception e){
        e.printStackTrace();
        return R.fail(e.getMessage());
    }

    /**
     * 自定义异常处理
     */
    @ExceptionHandler(value = GlobalException.class)
    @ResponseBody
    public Object myExceptionHandler(GlobalException e) {
        // 返回异常信息
        return R.fail(e.getMessage());
    }
}

